package scala.day03

import scala.io.Source

object Demo01StudentTest {
  def main(args:Array[String]):Unit = {
    /**
      * 统计班级人数
      */
    //读取数据,toList是为了保证读取一次数据多次使用
    val students: List[String] = Source.fromFile("data/students.txt").getLines().toList

    //取出班级
    val clazzs: List[String] = students.map(student =>student.split(",")(4))

    //按照班级分组
    val group: Map[String, List[String]] = clazzs.groupBy(clazz =>clazz)

    //统计班级人数
    val clazzNum: Map[String, Int] = group.map(kv =>{
      val clazz: String = kv._1
      val clazzList: List[String] = kv._2
      //计算班级人数
      val num: Int = clazzList.length
      (clazz,num)
    })
    clazzNum.toList.sortBy(f => -f._2).foreach(println)
  }
}
